var utils = require("../../utils/utils");
var appUtils = require("../../utils/app");
var ocrGml = require("../../utils/ocrGml");
var ACTION = require("../../base/action");
var common = require("./common");
var nextVideo = require("../appCommon/nextVideo");
var aWatchVideo = new ACTION("刷视频", {
  storage: common.storage,
});
aWatchVideo.close = function () {
  appUtils.close();
  sleep(utils.getRandom(1.5, 3, 1000));
  home();
};
aWatchVideo.checkIfLive = function () {
  var contents = [
    //prettier-ignore
    "点击进入",
    "直播间",
    "点击重播",
  ];
  if (ocrGml.captureExistOne(contents)) {
    return true;
  }
  var content = "后开始";
  if (utils.existsTextEndsWith(content)) {
    return true;
  }
  return false;
};
aWatchVideo.exec = function () {
  console.show(true);
  var startTime = Date.now();
  var count = 0;
  var baseSpan = 60 * 60 + 10 * 60;
  var span = utils.getRandom(baseSpan, baseSpan + 20);
  while (true) {
    if (this.checkIfLive()) {
      console.log("nextVideo");
      nextVideo();
    }
    var checkSpan = 20;
    sleep(utils.getRandom(checkSpan, checkSpan + 2, 1000));
    var now = Date.now();
    var hasWatch = (now - startTime) / 1000;
    count++;
    if (count % 5 == 0) {
      console.log("已经看了-" + hasWatch + "/" + span + "-s");
    }
    if (hasWatch >= span) {
      this.close();
      break;
    }
  }
  console.hide();
};

module.exports = aWatchVideo;
